<?php

class DefaultController extends Controller
{

		private $_model;

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','check','sorce'),
				'users'=>array('@'),
				'expression'=>'in_array(Yii::app()->user->getState("roles"),array(2))',	
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
	
	public function actionIndex()
	{
		//$model=Exams::model()->with('checks')->findAll();
		$model = new Exams();
		$model->unsetAttributes();  // clear any default values
		//考试的列表：状态为开启，考生没有参加的考试
		$model->exams_statuts = 1;
//		if(isset($_GET['state']))
//		{
//			$model->checks->checks_user_id = Yii::app()->user->id;
//			$model->checks->checks_status = $_GET['state'];
//		}else{
//			$model->checks->checks_user_id != Yii::app()->user->id;  
//		}
//		if(isset($_GET['state'])){
//			$newData = array('checks_user_id'=>Yii::app()->user->id,'checks_status'=>$_GET['state']);
//		}else{
//			$newData = array('checks_user_id'=>Yii::app()->user->id);
//		}
		if(isset($_GET['Exams']))
			$model->attributes=$_GET['Exams'];
		$this->render('index',array(
			'model'=>$model,
//			'newDate'=>$newData,
		));
	}
	/**
	 * 成绩计算说明：判断每题是否正确，并且把正确的题的分数占分比累加，乘以试卷总分即是总分
	 * 排名说明：将考生考试表中所有相同考试的成绩取出，并进行排名，然后进行更新
	 */
	public function actionCheck() {
		$answerRs = "";
		$userId = Yii::app()->user->id;
		$checkId = $_GET['checkId'];
		$checksId = Checks::model()->find(array(
		    'condition'=>'checks_user_id=:userID AND checks_tests_id=:checkID',
		    'params'=>array(':userID'=>$userId,':checkID'=>$checkId),
		));
		//无论是提交还是保存，考生考试表和考生答题表都要更新或者插入数据。两者只是状态或者时间的不同
		//先查看考生考试表是否存在此考生id和考试id对应的记录，没有则插入相关数据
		//然后检查考生答卷表是否存在考生考试id和对应试题ID，没有则插入相关数据。
		//pecho($_POST);pecho($_GET);exit;
		if(isset($_POST['TestsQuestion'])){
			if(isset($_POST['yt0'])){
				$submitTime = date("Y-m-d H:i:s");
				$state = 2;
			}else{
				$submitTime = '';
				$state = 1;
			}
			if($checksId){
				$checksId->checks_submit_time = $submitTime;
				$checksId->checks_status = $state;
				$sroceAll = 0;
				//答卷更新
				foreach ($checksId->answers as $model) {
					//答卷是否有值 有则更新，没有赋予原来的值
					if(isset($_POST['TestsQuestion'][$model->answers_qid]['answer'])){
						$v = $_POST['TestsQuestion'][$model->answers_qid]['answer'];
					}else{
						$v = $model->answers_result;
					}
					if(is_array($v)){
						$model->answers_result = implode('#,',$v);
					}else{
						$model->answers_result = $v;
					}
					$model->save();
					$sroceAll += TestsQuestion::model()->getScore($model->answers_qid,$model->answers_result);
				}
				$checksId->checks_score = $sroceAll;
				$checksId->save();
				Checks::model()->updateRang($checksId->checks_tests_id);
			}else{
				$checksId = new Checks();
				$checksId->checks_user_id = $userId;
				$checksId->checks_tests_id = $checkId;
				$checksId->checks_submit_time = $submitTime;
				$checksId->checks_create_time = $_POST['checks_create_time'];
				$checksId->checks_status = $state;
				$checksId->save();
				$checkId = $checksId->checks_id;
				$sroceAll = 0;
				//答卷存储
				foreach ($_POST['TestsQuestion'] as $k =>$v) {
					$model = new Answers();
					$model->answers_checks_id = $checkId;
					$model->answers_qid = $k;
					if(is_array($v['answer'])){
						$model->answers_result = implode('#,',$v['answer']);
					}else{
						$model->answers_result = $v['answer'];
					}
					$model->save();
					$sroceAll += TestsQuestion::model()->getScore($model->answers_qid,$model->answers_result);
				}
				$checksId->checks_score = $sroceAll;
				$checksId->save();
				Checks::model()->updateRang($checksId->checks_tests_id);
			}
			$this->redirect(array('index'));
		}else{
			//生成考卷
			if($checksId)//试卷已保存或者提交 读取已答结果
			{
				$rs = Answers::model()->findAll('answers_checks_id=:answers_checks_id',array('answers_checks_id'=>$checksId->checks_id));
				foreach ($rs as $v) {
					$answerRs[$v->answers_qid] = $v->answers_result ;
				}
			}
		}
		$model = Exams::model()->findByPk($_GET['checkId']);
		$this->render('answer',array('model'=>$model,'rs'=>$answerRs));
	}
	public function actionSorce(){
		$model = new Checks();
		$model->checks_user_id = Yii::app()->user->id;
		$model->checks_status = 2;
		$this->render('sorce',array('model'=>$model));
	}
}